package com.team2.cyber_eye;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.AsyncTask.Status;
import android.view.ContextThemeWrapper;

import com.team2.cyber_eye.ActivityHelpers.Util;
import com.team2.cyber_eye.background_task.BackgroundTask;
import com.team2.cyber_eye.background_task.BackgroundTask.LoadingTaskFinishedListener;
import com.team2.shared.Product;
//import com.team2.shoppingService.Shopper;

public class Processing extends CyberActivity implements LoadingTaskFinishedListener {
	
	Activity parent = this;
	private String searchText = "";
	private String fromIQE = "";
	private String category = "";
	private String filePath = "";
	private String iqeOrBarcode = "";
	private ProgressDialog progDialog;
	private BackgroundTask BT;
	private long timeout = 180000;

	@SuppressWarnings("deprecation")//we support versions older than 13 so suppress the warning
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.activity_processing, "PROCESSING");

		try
		{
			if(Options.getSearchTimeout() > 0)
			{
				timeout = TimeUnit.MILLISECONDS.convert(Options.getSearchTimeout(), TimeUnit.MINUTES);
			}
		}
		catch(Throwable ex)
		{
			//WTF android????????
		}
		
		showDialog(0);
	  
		Bundle extras = this.getIntent().getExtras();
		
		if (extras != null)
		{
			ArrayList<String> list = new ArrayList<String>();
			
			searchText = extras.getString("text");
			category = extras.getString("category");
			fromIQE = extras.getString("fromIQE");
			filePath = extras.getString("filePath");
			iqeOrBarcode = extras.getString("iqeOrBarcode");
			
			list.add(searchText);
			list.add(category);
			list.add(fromIQE);
			list.add(iqeOrBarcode);
			list.add(filePath);
			
	        BT = new BackgroundTask(this, this);
	        BT.execute(list);
	        
	        Handler handler = new Handler();
			handler.postDelayed(new Runnable()
			{
				@Override
				public void run() {
					if ( !Util.isTaskCompleted() && BT.getStatus() == Status.RUNNING )
					{
						BT.cancel(true);
						progDialog.dismiss();
						
						new AlertDialog.Builder(new ContextThemeWrapper(getApplicationContext(), R.style.Theme_MyDialog))
			            .setIcon(android.R.drawable.ic_dialog_alert)
			            .setTitle("")
			            .setMessage("Server is busy, try again later")
			            .setNeutralButton("OK", new DialogInterface.OnClickListener()
				        {
				            @Override
				            public void onClick(DialogInterface dialog, int which) {
				            	parent.finish();    
				            }
				
				        })
				        .show();
					}			
				}
			}, timeout );
	    }
	}

	@Override
	protected Dialog onCreateDialog(int id){
		 progDialog = new ProgressDialog(this, R.style.Theme_MyDialog);
		 progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		 progDialog.setTitle("In Progress");
	     progDialog.setMessage("Searching Products...");	
	     progDialog.setIcon(null);
	     return progDialog;
	}
	
	@Override
	public void onTaskFinished(ArrayList<ArrayList<Product>> products)
	{
		
    	if(products == null)
    	{
    		Util.DisplayIssue(this, "Server Error!\nPlease wait 5 seconds and try again.");
    	}
    	else
    	{
    		ArrayList<Product> onlineProducts = products.get(0);
    		ArrayList<Product> localStoreProducts = products.get(1);
    		
    		Intent intent = new Intent();
        	intent.setClass(this, ProductTabsView.class);
        	Bundle p1 = new Bundle();
            p1.putParcelableArrayList("onlineProducts", onlineProducts);
        	intent.putExtras(p1);
        	
        	Bundle p2 = new Bundle();
        	p2.putParcelableArrayList("localStoreProducts", localStoreProducts);
        	intent.putExtras(p2);
        	
        	startActivity(intent);
    	}
    	
    	//remove this activity from activity history
    	finish();
	}

}
